<%-- 
    Document   : LikeComment
    Created on : Dec 9, 2011, 4:27:31 AM
    Author     : hendrikde
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Like Unlike</title>
    </head>
    <body>
                <%
        int comment_id = Integer.parseInt(request.getParameter("COMM"));
        int user_id = Integer.parseInt(session.getAttribute("UserID").toString());
        int circle_id = Integer.parseInt(request.getParameter("CIRC"));
        
       DBClasses.GetConnection gc = new DBClasses.GetConnection();
       java.sql.Connection conn = gc.getConnection();
       
       try {
           java.sql.Statement stmt1 = conn.createStatement();
           
           String query1 = "SELECT * "
                   + "FROM comment_likes C "
                   + "WHERE C.CommentID=" +comment_id +" AND C.CustomerID=" +user_id;
           
           java.sql.ResultSet rs1 = stmt1.executeQuery(query1);
           int count = 0;
           while (rs1.next()) {
               count++;
           }
           
           if (count>0) {
               conn.createStatement().execute("DELETE FROM comment_likes WHERE CommentID=" + comment_id +" AND CustomerID=" +user_id);
           }
           else {
               conn.createStatement().execute("INSERT INTO comment_likes(CommentID, CustomerID) "
                       + "VALUES("+comment_id+"," +user_id +")");
            }
       }
       catch(Exception e) {
           e.printStackTrace();
           session.setAttribute("CIRC", circle_id);
           response.sendRedirect("CirclePage.jsp");
       }
       
        finally
    {
        try{
            conn.commit();
            conn.close();
            session.setAttribute("CIRC", circle_id);
            response.sendRedirect("CirclePage.jsp");
        }
        catch(Exception ee)
                               {
            ee.printStackTrace();
        }
    }
        
        %>
    </body>
</html>
